﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace DailyNote
{
    /// <summary>
    /// 邮件操作类
    /// </summary>
    public class MailHelper
    {
        /// <summary>
        /// 获取Email登陆地址
        /// </summary>
        /// <param name="email">email地址</param>
        /// <returns></returns>
        public static string GetEMailLoginUrl(string email)
        {
            if ((email == string.Empty) || (email.IndexOf("@") <= 0))
            {
                return string.Empty;
            }
            int index = email.IndexOf("@");
            email = "http://mail." + email.Substring(index + 1);
            return email;
        }
        /// <summary>
        /// 发送邮件（要求登陆）
        /// </summary>
        /// <param name="mailSubjct">邮件主题</param>
        /// <param name="mailBody">邮件正文</param>
        /// <param name="mailFrom">发送者</param>
        /// <param name="mailAddress">接收地址列表</param>
        /// <param name="HostIP">主机IP</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public static bool SendMail(string mailSubjct, string mailBody, string mailFrom, string password, List<string> mailAddress, string HostIP)
        {
            bool flag;
            string str = SendMail(mailSubjct, mailBody, mailFrom, password, mailAddress, HostIP, out flag);
            return flag;
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="mailFrom">谁发布的</param>
        /// <param name="mailAddress">发布给谁</param>
        /// <param name="HostIP">主机 "smtp.yeah.net"</param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public static string SendMail(string mailSubjct, string mailBody, string mailFrom, string password, List<string> mailAddress, string HostIP, out bool sendOK)
        {
            sendOK = true;
            string str = "";
            try
            {
                if (string.IsNullOrEmpty(mailSubjct) || string.IsNullOrEmpty(mailBody))
                    return str;

                //设置邮箱的地址或IP //设置邮箱端口，pop3端口:110, smtp端口是:25   
                SmtpClient client = new SmtpClient();
                client.Host = HostIP;
                client.Port = 25;
                client.Timeout = 9999;

                MailMessage mm = new MailMessage();
                mm.IsBodyHtml = false;
                mm.Priority = MailPriority.High;
                mm.Subject = mailSubjct;
                mm.Body = mailBody;
                mm.From = new MailAddress(mailFrom);

                Regex regex = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
                for (int i = 0; i < mailAddress.Count; i++)
                {
                    if (regex.IsMatch(mailAddress[i]))
                    {
                        mm.To.Add(mailAddress[i]);
                    }
                }
                if (mm.To.Count == 0)
                {
                    return string.Empty;
                }

                //要输入邮箱用户名与密码  
                client.Credentials = new NetworkCredential(mailFrom, password);
                client.Send(mm);
            }
            catch (Exception exception)
            {
                str = exception.Message;
                sendOK = false;
            }
            return str;
        }
    }
}
